package com.hrt.thirtyparty.sncode;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.SimpleDoc;
import javax.print.attribute.Attribute;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.PrinterName;

import org.krysalis.barcode4j.impl.code39.Code39Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;

public class SNCodePrinter   {

	
	private SNCodePrintService sncodePrintService;
	
	public List<File> genaranteSNCodeImgs(List<String> sncodes) {

		List<File> files = new ArrayList<File>();
		try {
			for (String snCode : sncodes) {

				File file = genarateSNCoe(snCode);

				files.add(file);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return files;

	}

	public File genarateSNCoe(String snCode) throws IOException {

		// Create the barcode bean
		Code39Bean bean = new Code39Bean();

		final int dpi = 150;

		// Configure the barcode generator
		bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); // makes the narrow bar
															// width exactly one
															// pixel
		bean.setWideFactor(3);
		bean.doQuietZone(false);

		// Open output file
		String fileName = genarateFileName(snCode);
		File outputFile = new File(fileName);
		OutputStream out = null;
		try {
			out = new FileOutputStream(outputFile);
			BitmapCanvasProvider canvas = new BitmapCanvasProvider(out,
					"image/jpeg", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);

			// Generate the barcode
			bean.generateBarcode(canvas, snCode);
			canvas.finish();
			// Signal end of generation
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			out.close();
		}

		return outputFile;

	}

	/**
	 * 生成文件名
	 * 
	 * @return
	 */
	private String genarateFileName(String snCode) {
		// TODO Auto-generated method stub
		SimpleDateFormat mf = new SimpleDateFormat("yyyy-MM-dd");
		String fileDate = mf.format(new Date());
		String fileName = "SN_" + fileDate + "_" + snCode + ".jpg";

		return fileName;
	}

	public void printSNCode(List<File> snCodeImgs) throws PrintException,
			IOException {

		for (File file : snCodeImgs) {

			
			if (file != null && file.isFile() && file.exists()) {
				sncodePrintService=new SNCodePrintService(file.getAbsolutePath());
				
				PageFormat pf = new PageFormat();
				  pf.setOrientation(PageFormat.PORTRAIT);
				  // 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
				  Paper p = new Paper();
				  p.setSize(182, 80);// 纸张大小
				  p.setImageableArea(0, 0, 1217, 177);// 设置打印区域
				  pf.setPaper(p);
				  Book book=new Book();
				// 把 PageFormat 和 Printable 添加到书中，组成一个页面
				  book.append(sncodePrintService, pf);
				  // 获取打印服务对象
				  PrinterJob job = PrinterJob.getPrinterJob();
				  if(job!=null){
				  // 设置打印类
				  job.setPageable(book);
				  try {
					job.print();
				  } catch (PrinterException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				  
				}else{
					throw new FileNotFoundException("文件打印出错");
				}
			} else {
				throw new FileNotFoundException("文件读取出错");

			}
		}

	}

	
}
